Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 470)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.37837 11.43381 11.48828 11.54179 11.59436 11.64599 11.69668 11.74646
## [9] 11.79533 11.84329 11.89036 11.93655 11.98186 12.02630 12.06989 12.11263
## [17] 12.15453 12.19559 12.23584 12.27527 12.31391 12.35174 12.38874 12.42489
## [25] 12.46016 12.49452 12.52795 12.56042 12.59192 12.62240 12.65185 12.68024
## [33] 12.70755 12.73375 12.75917 12.78407 12.80840 12.83205 12.85494 12.87700
## [41] 12.89814 12.91828 12.93733 12.95521 12.97184 12.98714 13.00101 13.01339
## [49] 13.02470 13.03541 13.04542 13.05466 13.06305 13.07051 13.07695 13.08231
## [57] 13.08649 13.08941 13.09101 13.09119 13.08988 13.08699 13.08112 13.07125
## [65] 13.05793 13.04168 13.02304 13.00255 12.98074 12.95815 12.93530 12.91274
## [73] 12.89100 12.87062 12.85212 12.83605 12.81905 12.79788 12.77320 12.74568
## [81] 12.71598 12.68478 12.65274 12.62053 12.58882 12.55827 12.52956 12.50335
## [89] 12.48031 12.46111 12.44310 12.42343 12.40245 12.38052 12.35801 12.33527
## [97] 12.31266 12.29054 12.26927 12.24920 12.23070 12.21412 12.19983 12.18818
## [105] 12.17897 12.17162 12.16593 12.16169 12.15868 12.15670 12.15555 12.15501
## [113] 12.15487 12.15494 12.15499 12.15483 12.15424 12.15301 12.15230 12.15323
## [121] 12.15557 12.15909 12.16356 12.16875 12.17443 12.18036 12.18632 12.19207
## [129] 12.19738 12.20202 12.20577 12.20838 12.20973 12.21002 12.20950 12.20841
## [137] 12.20702 12.20557 12.20430 12.20348 12.20336 12.20418 12.20620 12.20966
## [145] 12.21483 12.22194 12.23088 12.24125 12.25289 12.26564 12.27934 12.29383
## [153] 12.30897 12.32457 12.34049 12.35657 12.37265 12.38857 12.40417 12.41929
## [161] 12.43378 12.44746 12.46259 12.48120 12.50284 12.52705 12.55338 12.58138
## [169] 12.61059 12.64057 12.67085 12.70099 12.73052 12.75901 12.78599 12.81101
## [177] 12.83362 12.85336 12.86978 12.88243 12.89086 12.89795 12.90664 12.91657
## [185] 12.92736 12.93866 12.95009 12.96129 12.97188 12.98151 12.98981 12.99640
## [193] 13.00092 13.00301 13.00229 12.99840 12.99097 12.97953 12.96414 12.94528
## [201] 12.92338 12.89890 12.87230 12.84403 12.81455 12.78431 12.75377 12.72338
## [209] 12.69359 12.66486 12.63765 12.60726 12.56950 12.52552 12.47652 12.42365
## [217] 12.36811 12.31106 12.25368 12.19715 12.14264 12.09133 12.04439 12.00299
## [225] 11.96832 11.93604 11.90141 11.86497 11.82726 11.78884 11.75024 11.71202
## [233] 11.67472 11.63889 11.60507 11.57381 11.54566 11.52115 11.50085 11.48342
## [241] 11.46720 11.45220 11.43843 11.42591 11.41464 11.40463 11.39591 11.38848
## [249] 11.38234 11.37753 11.37403 11.37188 11.37108 11.37343 11.38031 11.39112
## [257] 11.40522 11.42200 11.44084 11.46112 11.48222 11.50353 11.52442 11.54428
## [265] 11.56248 11.57840 11.59144 11.60438 11.62016 11.63836 11.65854 11.68029
## [273] 11.70319 11.72680 11.75071 11.77448 11.79771 11.81996 11.84081 11.85983
## [281] 11.87661 11.89249 11.90903 11.92613 11.94368 11.96156 11.97967 11.99789
## [289] 12.01612 12.03424 12.05216 12.06974 12.08690 12.10351 12.11947 12.13474
## [297] 12.14939 12.16347 12.17705 12.19017 12.20289 12.21527 12.22735 12.23919
## [305] 12.25084 12.26237 12.27381 12.28524 12.29670 12.30824 12.31992 12.33180
## [313] 12.34392 12.35635 12.36950 12.38364 12.39860 12.41420 12.43026 12.44659
## [321] 12.46303 12.47938 12.49548 12.51115 12.52620 12.54045 12.55374 12.56587
## [329] 12.57769 12.59006 12.60285 12.61593 12.62916 12.64242 12.65557 12.66848
## [337] 12.68102 12.69305 12.70444 12.71507 12.72480 12.73349 12.74104 12.74748
## [345] 12.75294 12.75755 12.76142 12.76468 12.76745 12.76984 12.77199 12.77401
## [353] 12.77602 12.77815 12.78052 12.78324 12.78614 12.78892 12.79156 12.79405
## [361] 12.79636 12.79846 12.80033 12.80195 12.80330 12.80435 12.80509 12.80548
## [369] 12.80550 12.80514 12.80341 12.79958 12.79401 12.78706 12.77908 12.77044
## [377] 12.76148 12.75257 12.74407 12.73633 12.72971 12.72457 12.72126 12.72015
## [385] 12.71998 12.71936 12.71840 12.71720 12.71589 12.71457 12.71335 12.71236
## [393] 12.71170 12.71149 12.71183 12.71285 12.71465 12.71735 12.72179 12.72849
## [401] 12.73705 12.74706 12.75812 12.76983 12.78179 12.79359 12.80484 12.81513
## [409] 12.82406 12.83123 12.83623 12.83867 12.83943 12.83968 12.83948 12.83885
## [417] 12.83784 12.83647 12.83481 12.83287 12.83070 12.82834 12.82583 12.82320
## [425] 12.82050 12.81776 12.81493 12.81193 12.80875 12.80540 12.80187 12.79815
## [433] 12.79423 12.79012 12.78580 12.78128 12.77654 12.77159 12.76642 12.76101
## [441] 12.75538 12.74951 12.74344 12.73720 12.73078 12.72416 12.71735 12.71032
## [449] 12.70306 12.69558 12.68785 12.67986 12.67162 12.66310 12.65429 12.64519
## [457] 12.63582 12.62622 12.61639 12.60634 12.59606 12.58556 12.57484 12.56391
## [465] 12.55275 12.54138 12.52980 12.51801 12.50600 12.49380
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 470)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.86497 10.94136 11.01645 11.09024 11.16270 11.23384 11.30363 11.37207
## [9] 11.43916 11.50486 11.56919 11.63212 11.69365 11.75376 11.81245 11.86970
## [17] 11.92550 11.97984 12.03272 12.08411 12.13400 12.18238 12.22930 12.27477
## [25] 12.31884 12.36153 12.40288 12.44292 12.48169 12.51921 12.55552 12.59065
## [33] 12.62463 12.65750 12.68885 12.71832 12.74601 12.77202 12.79647 12.81946
## [41] 12.84108 12.86146 12.88068 12.89886 12.91610 12.93251 12.94819 12.96324
## [49] 12.97654 12.98708 12.99514 13.00100 13.00495 13.00727 13.00825 13.00816
## [57] 13.00730 13.00595 13.00438 13.00289 13.00176 13.00127 13.00031 12.99769
## [65] 12.99359 12.98815 12.98155 12.97396 12.96553 12.95643 12.94683 12.93689
## [73] 12.92678 12.91665 12.90668 12.89703 12.88729 12.87695 12.86602 12.85449
## [81] 12.84237 12.82966 12.81637 12.80248 12.78802 12.77297 12.75734 12.74113
## [89] 12.72435 12.70700 12.68732 12.66398 12.63757 12.60868 12.57791 12.54585
## [97] 12.51309 12.48024 12.44789 12.41662 12.38704 12.35975 12.33532 12.31437
## [105] 12.29335 12.26878 12.24138 12.21187 12.18095 12.14933 12.11773 12.08686
## [113] 12.05743 12.03015 12.00574 11.98490 11.96834 11.95679 11.94852 11.94135
## [121] 11.93525 11.93020 11.92617 11.92315 11.92110 11.92002 11.91987 11.92063
## [129] 11.92228 11.92479 11.92816 11.93234 11.93847 11.94745 11.95897 11.97267
## [137] 11.98824 12.00533 12.02360 12.04273 12.06238 12.08222 12.10191 12.12111
## [145] 12.13950 12.15673 12.17569 12.19909 12.22633 12.25681 12.28995 12.32515
## [153] 12.36181 12.39936 12.43718 12.47469 12.51130 12.54641 12.57942 12.60976
## [161] 12.63681 12.66000 12.68209 12.70608 12.73166 12.75854 12.78643 12.81503
## [169] 12.84404 12.87318 12.90215 12.93065 12.95839 12.98508 13.01041 13.03410
## [177] 13.05585 13.07536 13.09235 13.10651 13.11756 13.12776 13.13937 13.15202
## [185] 13.16537 13.17906 13.19274 13.20606 13.21866 13.23019 13.24030 13.24864
## [193] 13.25484 13.25857 13.25947 13.25718 13.25135 13.24194 13.22935 13.21390
## [201] 13.19594 13.17578 13.15376 13.13020 13.10545 13.07981 13.05364 13.02725
## [209] 13.00097 12.97513 12.95007 12.92240 12.88908 12.85096 12.80887 12.76368
## [217] 12.71621 12.66732 12.61786 12.56866 12.52058 12.47445 12.43113 12.39146
## [225] 12.35628 12.32158 12.28323 12.24189 12.19824 12.15295 12.10670 12.06014
## [233] 12.01397 11.96883 11.92542 11.88439 11.84642 11.81218 11.78235 11.75391
## [241] 11.72375 11.69233 11.66014 11.62767 11.59540 11.56380 11.53337 11.50458
## [249] 11.47792 11.45387 11.43291 11.41552 11.40219 11.39275 11.38649 11.38307
## [257] 11.38219 11.38352 11.38672 11.39147 11.39746 11.40435 11.41181 11.41954
## [265] 11.42719 11.43444 11.44097 11.44876 11.45973 11.47348 11.48961 11.50771
## [273] 11.52738 11.54822 11.56982 11.59178 11.61370 11.63517 11.65580 11.67517
## [281] 11.69289 11.71120 11.73235 11.75591 11.78146 11.80860 11.83689 11.86592
## [289] 11.89527 11.92452 11.95325 11.98105 12.00749 12.03215 12.05462 12.07629
## [297] 12.09876 12.12191 12.14561 12.16974 12.19420 12.21884 12.24357 12.26825
## [305] 12.29277 12.31700 12.34083 12.36414 12.38680 12.40869 12.42971 12.44972
## [313] 12.46860 12.48624 12.50303 12.51944 12.53548 12.55117 12.56652 12.58155
## [321] 12.59627 12.61069 12.62483 12.63870 12.65231 12.66568 12.67883 12.69175
## [329] 12.70433 12.71642 12.72808 12.73935 12.75025 12.76084 12.77115 12.78122
## [337] 12.79110 12.80083 12.81043 12.81997 12.82946 12.83896 12.84795 12.85595
## [345] 12.86310 12.86952 12.87534 12.88069 12.88569 12.89048 12.89518 12.89991
## [353] 12.90482 12.91002 12.91564 12.92181 12.92840 12.93516 12.94205 12.94901
## [361] 12.95601 12.96300 12.96992 12.97675 12.98342 12.98989 12.99612 13.00207
## [369] 13.00767 13.01290 13.01766 13.02197 13.02587 13.02945 13.03276 13.03587
## [377] 13.03885 13.04177 13.04469 13.04767 13.05080 13.05412 13.05771 13.06163
## [385] 13.06597 13.07071 13.07575 13.08101 13.08640 13.09182 13.09719 13.10241
## [393] 13.10741 13.11208 13.11634 13.12010 13.12327 13.12575 13.12843 13.13209
## [401] 13.13650 13.14142 13.14664 13.15192 13.15704 13.16176 13.16586 13.16911
## [409] 13.17128 13.17214 13.17146 13.16902 13.16488 13.15937 13.15267 13.14494
## [417] 13.13637 13.12713 13.11740 13.10735 13.09715 13.08699 13.07703 13.06745
## [425] 13.05843 13.05014 13.04203 13.03345 13.02445 13.01505 13.00529 12.99520
## [433] 12.98483 12.97420 12.96336 12.95233 12.94116 12.92987 12.91850 12.90710
## [441] 12.89569 12.88431 12.87281 12.86105 12.84904 12.83678 12.82429 12.81158
## [449] 12.79865 12.78552 12.77220 12.75870 12.74503 12.73120 12.71723 12.70312
## [457] 12.68890 12.67459 12.66018 12.64566 12.63102 12.61626 12.60136 12.58632
## [465] 12.57112 12.55576 12.54022 12.52451 12.50860 12.49249
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 470)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.86903 10.92783 10.98565 11.04250 11.09836 11.15323 11.20711 11.26001
## [9] 11.31191 11.36282 11.41273 11.46164 11.50955 11.55645 11.60235 11.64724
## [17] 11.69112 11.73398 11.77584 11.81667 11.85650 11.89533 11.93316 11.97001
## [25] 12.00585 12.04071 12.07457 12.10743 12.13930 12.17017 12.20005 12.22894
## [33] 12.25683 12.28373 12.30947 12.33393 12.35716 12.37921 12.40013 12.41997
## [41] 12.43879 12.45663 12.47355 12.48960 12.50483 12.51929 12.53304 12.54611
## [49] 12.55843 12.56985 12.58040 12.59006 12.59884 12.60675 12.61378 12.61994
## [57] 12.62524 12.62967 12.63324 12.63594 12.63780 12.63879 12.63809 12.63501
## [65] 12.62985 12.62287 12.61436 12.60461 12.59388 12.58246 12.57062 12.55866
## [73] 12.54684 12.53545 12.52477 12.51507 12.50424 12.49029 12.47370 12.45496
## [81] 12.43455 12.41295 12.39066 12.36814 12.34588 12.32437 12.30409 12.28552
## [89] 12.26914 12.25545 12.24263 12.22869 12.21384 12.19828 12.18222 12.16587
## [97] 12.14944 12.13312 12.11713 12.10168 12.08696 12.07319 12.06057 12.04930
## [105] 12.03913 12.02959 12.02065 12.01223 12.00428 11.99674 11.98956 11.98269
## [113] 11.97605 11.96960 11.96329 11.95704 11.95081 11.94454 11.93827 11.93211
## [121] 11.92608 11.92020 11.91450 11.90901 11.90375 11.89874 11.89401 11.88959
## [129] 11.88549 11.88175 11.87838 11.87542 11.87174 11.86644 11.85990 11.85247
## [137] 11.84453 11.83645 11.82858 11.82131 11.81499 11.80998 11.80667 11.80542
## [145] 11.80658 11.81054 11.81631 11.82268 11.82965 11.83721 11.84536 11.85411
## [153] 11.86344 11.87335 11.88385 11.89493 11.90660 11.91884 11.93165 11.94504
## [161] 11.95900 11.97353 11.99080 12.01262 12.03846 12.06778 12.10005 12.13474
## [169] 12.17131 12.20924 12.24798 12.28700 12.32578 12.36377 12.40045 12.43528
## [177] 12.46773 12.49726 12.52334 12.54544 12.56303 12.58006 12.60044 12.62356
## [185] 12.64879 12.67554 12.70318 12.73109 12.75867 12.78530 12.81036 12.83324
## [193] 12.85332 12.86999 12.88263 12.89064 12.89339 12.89133 12.88560 12.87658
## [201] 12.86469 12.85031 12.83385 12.81571 12.79629 12.77599 12.75520 12.73433
## [209] 12.71378 12.69395 12.67523 12.65304 12.62334 12.58729 12.54609 12.50092
## [217] 12.45297 12.40343 12.35349 12.30432 12.25711 12.21306 12.17334 12.13915
## [225] 12.11167 12.08702 12.06076 12.03325 12.00484 11.97587 11.94669 11.91766
## [233] 11.88912 11.86141 11.83490 11.80993 11.78684 11.76599 11.74773 11.73148
## [241] 11.71639 11.70239 11.68943 11.67745 11.66636 11.65613 11.64667 11.63793
## [249] 11.62984 11.62235 11.61539 11.60888 11.60278 11.59785 11.59479 11.59340
## [257] 11.59352 11.59497 11.59757 11.60115 11.60551 11.61049 11.61591 11.62159
## [265] 11.62735 11.63302 11.63841 11.64464 11.65279 11.66259 11.67376 11.68605
## [273] 11.69919 11.71292 11.72698 11.74109 11.75499 11.76842 11.78111 11.79281
## [281] 11.80323 11.81310 11.82328 11.83373 11.84444 11.85536 11.86648 11.87777
## [289] 11.88919 11.90072 11.91233 11.92400 11.93568 11.94737 11.95902 11.97159
## [297] 11.98591 12.00178 12.01899 12.03733 12.05659 12.07657 12.09704 12.11782
## [305] 12.13868 12.15942 12.17983 12.19970 12.21882 12.23699 12.25400 12.26963
## [313] 12.28369 12.29595 12.30681 12.31683 12.32609 12.33465 12.34259 12.34996
## [321] 12.35685 12.36332 12.36944 12.37527 12.38090 12.38639 12.39180 12.39721
## [329] 12.40222 12.40646 12.41000 12.41295 12.41538 12.41739 12.41906 12.42049
## [337] 12.42176 12.42297 12.42420 12.42553 12.42707 12.42889 12.43055 12.43158
## [345] 12.43207 12.43211 12.43178 12.43117 12.43036 12.42945 12.42851 12.42764
## [353] 12.42692 12.42644 12.42627 12.42652 12.42621 12.42446 12.42153 12.41764
## [361] 12.41305 12.40798 12.40268 12.39739 12.39235 12.38779 12.38396 12.38109
## [369] 12.37943 12.37921 12.37986 12.38065 12.38159 12.38270 12.38400 12.38550
## [377] 12.38722 12.38918 12.39139 12.39387 12.39664 12.39971 12.40310 12.40682
## [385] 12.41163 12.41808 12.42590 12.43484 12.44462 12.45497 12.46563 12.47634
## [393] 12.48682 12.49682 12.50605 12.51426 12.52118 12.52654 12.53172 12.53811
## [401] 12.54547 12.55354 12.56208 12.57083 12.57956 12.58801 12.59594 12.60310
## [409] 12.60923 12.61410 12.61745 12.61904 12.61902 12.61780 12.61554 12.61239
## [417] 12.60851 12.60405 12.59916 12.59401 12.58874 12.58350 12.57846 12.57376
## [425] 12.56957 12.56602 12.56263 12.55878 12.55452 12.54989 12.54492 12.53965
## [433] 12.53412 12.52837 12.52242 12.51633 12.51013 12.50384 12.49752 12.49120
## [441] 12.48491 12.47869 12.47240 12.46586 12.45908 12.45208 12.44488 12.43750
## [449] 12.42993 12.42221 12.41434 12.40633 12.39821 12.38999 12.38168 12.37330
## [457] 12.36487 12.35639 12.34787 12.33928 12.33062 12.32188 12.31304 12.30410
## [465] 12.29504 12.28586 12.27654 12.26707 12.25745 12.24766
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")